import random
import time
import threading

import uiautomator2 as u2
from adbutils import adb

# device_1 = '10.10.113.54:5555'
device_1 = '192.168.3.17:5555'
device_2 = '192.168.3.17:5555'
device_3 = '192.168.3.17:5555'


class DeviceConnection:
    def __init__(self, serial):
        self.serial = serial
        self.device = None
        self.adb_device = None
        self.connect()
        self.monitor_thread = threading.Thread(target=self.monitor_connection)
        self.monitor_thread.daemon = True
        self.monitor_thread.start()

    def connect(self):
        while True:
            try:
                self.device = u2.connect(self.serial)
                self.adb_device = adb.device(serial=self.serial)
                print(f"Connected to {self.serial}")
                break
            except Exception as e:
                print(f"Failed to connect to {self.serial}, retrying in 5 seconds... Exception: {e}")
                time.sleep(5)

    def monitor_connection(self):
        while True:
            try:
                if not self.device.healthcheck():
                    print(f"Device {self.serial} is disconnected, reconnecting...")
                    self.connect()
            except Exception as e:
                print(f"Error checking health of {self.serial}: {e}")
            time.sleep(10)


device_one = DeviceConnection(device_1)
device_two = DeviceConnection(device_2)
device_three = DeviceConnection(device_3)
d_one = device_one.device
d_two = device_two.device
d_three = device_three.device
d_one_device = device_one.adb_device
d_two_device = device_two.adb_device
d_three_device = device_three.adb_device
# d_one = u2.connect(device_1)
# d_two = u2.connect(device_2)
# d_three = u2.connect(device_3)
# d_one_device = adb.device(serial=device_1)
# d_two_device = adb.device(serial=device_2)
# d_three_device = adb.device(serial=device_3)

if __name__ == '__main__':
    # adb_device.root()
    # adb_device.shell([r"sendevent /dev/input/event0 1 353 1", r"sendevent /dev/input/event0 0 0 0"])
    # adb_device.shell(r"sendevent /dev/input/event0 1 353 1")
    # res = adb_device.shell("< test.txt")
    # res = adb_device.shell("./data/test.sh")
    # res = adb_device.shell(
    #     "su & sendevent /dev/input/event0 1 353 1 & sendevent /dev/input/event0 0 0 0 & echo 'down' & sleep 3 & sendevent /dev/input/event0 1 353 0 & sendevent /dev/input/event0 0 0 0 & echo 'up'")
    # print(res)
    # res = d_one.app_current()
    # d_one(text="开启投屏").click()
    # print(res)
    # pass
    res = d_one.app_current()
    print(res)
    # res = d_one_device.shell("am uninstall com.arcvideo.totalmedia.cpdemo.Tencent")
    # while True:
    #     Direction=random.choice(["up", "down"])
    #     time.sleep(2)
    #     d_three.swipe_ext(Direction)
    # print(d_one_device.shell("date '+%Y-%m-%d %H:%M:%S.%3N'"))
    # print(d_two_device.shell("date '+%Y-%m-%d %H:%M:%S.%3N'"))
    # d_one_device.shell(
    #     "am start --user 10 --display 1 com.arcvideo.totalmedia.cpuidemo.IQIYI/com.arcvideo.totalmedia.cpuidemo.MainActivity")
    # d_one_device.shell(
    #     "am start --user 10 --display 1 com.arcvideo.totalmedia.cpdemo.Youku/com.arcvideo.totalmedia.cpdemo.activity.MainActivity")
